package com.itheima.Day16.Day_1;

import java.io.*;
import java.text.DecimalFormat;

public class CopyFile extends Thread {
    private File srcFile;
    private File destFile;

    public CopyFile(String srcFile, String destFile) {
        this.srcFile = new File(srcFile);
        this.destFile = new File(destFile);
    }

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            bos = new BufferedOutputStream(new FileOutputStream(destFile));
            int len;
            long total_len = srcFile.length();
            System.out.println(total_len);
            System.out.printf("total_len=" + total_len);
            byte[] arr = new byte[1024*100];
            int length = 0;
            DecimalFormat df = new DecimalFormat("##%");
            while ((len = bis.read(arr)) != -1) {
                System.out.println(len);
                Thread.sleep(100);
                bos.write(arr, 0, len);
                length += len;
//                System.out.println(srcFile.getName() + "文件复制了：" + df.format(1.0 * length / total_len));
            }
            System.out.println(srcFile.getName() + "文件复制完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
